<template>
  <u-row
      class="progress-bar"
      @click.native.stop="onTableClick"
      style="background: white"
  >
    <u-col
        ref="title"
        class="title"
        v-if="title != null && title !== ''"
        span="12"
        :style="{
		  'color': options.title.textStyle.color,
		  'font-size': options.title.textStyle.fontSize + 'px',
		  'font-weight': options.title.bold ? 600 : undefined,
		  'text-align': options.title.left,
		  'font-style': options.title.italics ? 'italic' : undefined
	  }"
    >
      {{ title }}
    </u-col>
    <u-col class="progress-bar-container" span="12">
      <view
          v-if="options && options.seriesSetting"
          class="progress-container"
          v-for="(item, index) in data.dataList"
          :key="index"
          @dblclick="onDblClick(item)"
      >
        <u-line-progress
            :percent="item.percentage"
            style="flex-grow: 1;"
            :show-percent="false"
            :striped="true"
            :striped-active="true"
            :active-color="options.seriesSetting.color"
        />
        <view
            class="progress-text"
            v-if="!options.seriesSetting.textInside"
            :style="{
			  'color': options.seriesSetting.textStyle.color,
			  'width': options.seriesSetting.textStyle.width + 'px',
			  'font-size': options.seriesSetting.textStyle.fontSize + 'px',
			  'font-weight': options.seriesSetting.textStyle.bold
			    ? 600 : undefined,
			  'font-style': options.seriesSetting.textStyle.italics
			    ? 'italic' : undefined
		  }"
        >
          <view
              class="text"
              v-html="outerTextFormat(percentage(item), item)"
          ></view>
        </view>
      </view>
    </u-col>
  </u-row>
</template>

<script>
import chartMixins from './chartMixins.js';

export default {
  name: 'progressBar',
  props: {
    // 透视表数据
    data: {
      type: [Object, Array]
    },
    // 透视表高度
    height: {
      type: String
    },
    // 透视表设置
    options: {
      type: Object
    },
    widget: {
      type: Object
    }
  },
  mixins: [chartMixins],
  components: {},
  data() {
    return {
      ready: false
    };
  },
  methods: {
    onTableClick() {
      this.$emit('click');
    },
    onDblClick(clickData) {
      this.$emit('dblclick', clickData);
    },
    textFormat(percentage, item) {
      if (!this.options.seriesSetting.textInside) return '';

      let text = this.getDataString('textColumn', '', item);
      return this.options.seriesSetting.textStyle.text
          .replace(/\{percent\}/gi, percentage)
          .replace(/\{text\}/gi, text);
    },
    outerTextFormat(percentage, item) {
      let text = this.getDataString('textColumn', '', item);
      return this.options.seriesSetting.textStyle.text
          .replace(/:/gi, (text) ? ':' : '') // 说明字段未绑定或无文字不显示冒号
          .replace(/\{percent\}/gi, percentage)
          .replace(/\{text\}/gi, text);
    },
    percentage(item) {
      try {
        if (!this.getDataPropertyName('progressColumn')) return 0;
        let percentage =
            Number(
                (
                    this.getDataNumber('progressColumn', item) /
                    this.options.seriesSetting.maxValue * 100
                ).toFixed(2)
            )
        return percentage > 100 ? 100 : percentage;
      } catch (e) {
        console.log(e);
        return 0;
      }
    },
    refresh() {
      this.ready = false;
      setTimeout(() => {
        this.ready = true;
      }, 100);
    }
  },
  computed: {
    title() {
      if (
          this.options == null ||
          this.options.title == null ||
          !this.options.title.show
      ) {
        return undefined;
      }
      return this.options ? this.options.title.text : undefined;
    },
    watchPercentage() {
      return [this.data.dataList]
    }
  },
  watch: {
    watchPercentage: {
      handler() {
        if (Array.isArray(this.data.dataList)) {
          for (let i = 0; i < this.data.dataList.length; i++) {
            this.data.dataList[i].percentage = this.percentage(this.data.dataList[i]);
          }
        }
      },
      deep: true,
      immediate: true
    }
  }
};
</script>

<style scoped lang="scss">
.progress-bar {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.progress-bar .title {
  padding: 0px 4rpx;
  height: 80rpx;
  line-height: 60rpx;
  border-bottom: solid 1px #f6f6f6;
  flex-grow: 0;
  flex-shrink: 0;
}

.progress-bar .progress-bar-container {
  height: 100%;
  display: inline-flex;
  flex-direction: column;
  justify-content: center;
  padding: 40rpx 20rpx 0px 20rpx;
  box-sizing: border-box;
  overflow: hidden;
  width: 100%;
}

.progress-container {
  display: inline-flex;
  text-align: center;
  width: 100%;
  position: relative;
}

.progress-container + .progress-container {
  margin-top: 40rpx;
}

.progress-text {
  flex-grow: 0;
  flex-shrink: 0;
  display: inline-block;
  vertical-align: middle;
  text-align: right;
}

.progress-text::before {
  content: "";
  height: 100%;
  width: 0px;
  display: inline-block;
  vertical-align: middle;
}

.progress-text .text {
  line-height: 40rpx;
  display: inline-block;
  vertical-align: middle;
}

.progress-bar ::v-deep .el-progress {
  flex-grow: 1;
}

::v-deep .el-progress-bar {
  flex-grow: 1;
}
</style>
